//
//  LCYEmoticonPickerViewController.m
//  IMByLinkUs
//
//  Created by lcy on 16/1/15.
//  Copyright © 2016年 lcy. All rights reserved.
//

#import "LCYEmoticonPickerViewController.h"
#import "NSBundle+Emojis.h"
#import "YYImage.h"
#import "LCYEmoticonPickerCollectionViewCell.h"
#import "LCYEmoticonPickerCollectionViewFlowLayout.h"

@interface LCYEmoticonPickerViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate>

/**
 *  emoticon的plist
 */
@property (strong, nonatomic) NSMutableArray *emoticonInfoArray;

@end

@implementation LCYEmoticonPickerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _isClickedEmoticonBtn = YES;
    
    
    self.LCYEmoticonCollectionView.pagingEnabled = YES;
    self.LCYEmoticonCollectionView.autoresizesSubviews = NO;
    self.LCYEmoticonCollectionView.showsHorizontalScrollIndicator = NO;
    
    self.LCYEmoticonCollectionView.dataSource = self;
    self.LCYEmoticonCollectionView.delegate = self;
    self.LCYEmoticonCollectionView.scrollEnabled = YES;
    
    [self.LCYEmoticonCollectionView registerClass:[LCYEmoticonPickerCollectionViewCell class] forCellWithReuseIdentifier:kLCYEmoticonCellReuseId];
}

- (void)viewWillLayoutSubviews
{
    self.view.frame = CGRectMake(0, kLCYScreenHeight - kLCYAttachmentViewAndEmoticonViewHeight, kLCYScreenWidth, kLCYAttachmentViewAndEmoticonViewHeight);
}

#pragma mark - 懒加载

- (NSMutableArray *)emoticonInfoArray
{
    if(_emoticonInfoArray == nil)
    {
        self.emoticonInfoArray = [NSMutableArray array];
        
        NSMutableArray *array = [NSMutableArray arrayWithArray:[[NSBundle mainBundle] emojisInfoFromDirectory:@"EmoticonQQ.bundle" ofType:@"plist" ofName:@"info"]];
        
        for(NSDictionary *dic in array)
        {
            for(NSString *key in dic)
            {
                NSString *value = dic[key];
                YYImage *img = [[NSBundle mainBundle] emojiFromDirectory:@"EmoticonQQ.bundle" ofType:@"gif" ofIndex:value];
                
                LCYEmoticonInfoModel *emoticonInfo = [[LCYEmoticonInfoModel alloc] initWithYYImage:img name:key index:value];
                
                [self.emoticonInfoArray addObject:emoticonInfo];
            }
            
        }
    }
    return _emoticonInfoArray;
}

#pragma mark - 数据源方法

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    NSInteger numOfPages = self.emoticonInfoArray.count / kLCYNumberOfEmojisPerPages;
    if(self.emoticonInfoArray.count % kLCYNumberOfEmojisPerPages == 0)
    {
        return numOfPages;
    }
    return numOfPages + 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if(section == self.emoticonInfoArray.count / kLCYNumberOfEmojisPerPages)
    {
        return self.emoticonInfoArray.count % kLCYNumberOfEmojisPerPages + 1;
    }
    return kLCYNumberOfEmojisPerPages;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LCYEmoticonPickerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kLCYEmoticonCellReuseId forIndexPath:indexPath];
    
    LCYEmoticonInfoModel *imgInfo = self.emoticonInfoArray[indexPath.section * (kLCYNumberOfEmojisPerPages - 1) + indexPath.row];
    YYImage *img = imgInfo.image;
    
    // 添加删除按钮
    if(indexPath.row == kLCYNumberOfEmojisPerPages - 1 || ((indexPath.section == self.emoticonInfoArray.count / kLCYNumberOfEmojisPerPages) && (indexPath.row == self.emoticonInfoArray.count % kLCYNumberOfEmojisPerPages)))
    {
        UIImage *img = [UIImage imageNamed:@"deleteEmoji"];
        NSData *imgData = UIImagePNGRepresentation(img);
        cell.img = [YYImage imageWithData:imgData];
    }else
    {
        cell.img = img;
    }
    
    return cell;
}

#pragma mark - 代理方法

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 判断是否发送删除消息
    if(indexPath.row == kLCYNumberOfEmojisPerPages - 1 || ((indexPath.section == self.emoticonInfoArray.count / kLCYNumberOfEmojisPerPages) && (indexPath.row == self.emoticonInfoArray.count % kLCYNumberOfEmojisPerPages)))
    {
        // 点击了删除按钮
        NSDictionary *dic = @{@"isNeedDelete" : @1};
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"shouldDelegateEmoticon" object:self userInfo:dic];
    }else
    {
        // 添加表情
        
        NSInteger index = (indexPath.section * kLCYNumberOfEmojisPerPages + indexPath.row) / kLCYNumberOfEmojisPerPages + indexPath.section * (kLCYNumberOfEmojisPerPages - 2) + indexPath.row;
        LCYEmoticonInfoModel *imgInfo = self.emoticonInfoArray[index];
        NSDictionary *dic = @{@"info" : imgInfo};
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"shouldGetEmoticon" object:self userInfo:dic];
    }
}


// 计算pageControl
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    self.LCYPageIndicateControl.currentPage = self.LCYEmoticonCollectionView.contentOffset.x / kLCYScreenWidth;
    
}


- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    self.LCYPageIndicateControl.currentPage = self.LCYEmoticonCollectionView.contentOffset.x / kLCYScreenWidth;
}

#pragma mark - 按钮点击事件

- (IBAction)willSendMessage:(id)sender
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"shouldSendMessage" object:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
